//
//  VisualView.m
//  礼物
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "VisualView.h"

@implementation VisualView
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH - 20, HEIGHT / 2);
    //设置item的行间距
    flowLayout.minimumLineSpacing = 20;
    //设置item的上,左,下,右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //collectionView
    self.myCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) collectionViewLayout:flowLayout];
    self.myCollectionView.backgroundColor = RGBA(236, 235, 241, 1);
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.showsHorizontalScrollIndicator = NO;
    self.myCollectionView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.myCollectionView];
    //注册cell
    [self.myCollectionView registerClass:[VisualCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.visualCellArray.count == 0)
    {
        return 0;
    }
    else
    {
        return self.visualCellArray.count;
    }
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //直接从cell重用池里取
    VisualCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.layer.masksToBounds = YES;//masksToBounds裁剪掉边框
    cell.layer.cornerRadius = 3;//设置圆角度
    
    VisualModel *model = [self.visualCellArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

//点击item触发方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    VisualModel *model = [self.visualCellArray objectAtIndex:indexPath.row];
    [self.delegate  pushDetailController:model];
}


/*
 
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
